﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure

Namespace Serializers

    Public Class FormatSerializerFactory
        Implements IFormatSerializerFactory
        Private jsonSerializerValue As IFormatSerializer
        Private jsonpSerializerValue As IFormatSerializer
        Private xmlSerializerValue As IFormatSerializer

        Private ReadOnly Property JsonSerializer() As IFormatSerializer
            Get
                If Me.jsonSerializerValue Is Nothing Then
                    Me.jsonSerializerValue = New JsonSerializer()
                End If

                Return Me.jsonSerializerValue
            End Get
        End Property

        Private ReadOnly Property JsonpSerializer() As IFormatSerializer
            Get
                If Me.jsonpSerializerValue Is Nothing Then
                    Me.jsonpSerializerValue = New JsonSerializer()
                End If

                Return Me.jsonpSerializerValue
            End Get
        End Property

        Private ReadOnly Property XmlSerializer() As IFormatSerializer
            Get
                If Me.xmlSerializerValue Is Nothing Then
                    Me.xmlSerializerValue = New XmlSerializer()
                End If

                Return Me.xmlSerializerValue
            End Get
        End Property

        Public Function GetSerializer(ByVal headers As NameValueCollection, ByVal queryString As NameValueCollection) As IFormatSerializer Implements IFormatSerializerFactory.GetSerializer
            ' Check content type too.
            Dim mimeType = If(headers("Accept"), HttpConstants.MimeApplicationAtomXml)
            Dim callbackName As String = String.Empty

            If HttpContext.Current IsNot Nothing Then
                callbackName = If(queryString("jsonCallback"), queryString("callback"))
            End If

            If mimeType.Contains(HttpConstants.MimeApplicationJson) Then
                Return If(String.IsNullOrWhiteSpace(callbackName), Me.JsonSerializer, Me.JsonpSerializer)
            End If

            If mimeType.Contains("text/xml") OrElse mimeType.Contains("application/xml") Then
                Return Me.XmlSerializer
            End If

            If Not String.IsNullOrWhiteSpace(callbackName) Then
                Return Me.JsonpSerializer
            End If

            Return Me.XmlSerializer
        End Function
    End Class
End Namespace